<template>
    <div>
        
        <el-col :span="24" class="header">
            <el-switch
                style="display: block;margin:20px 0;width:200px"
                v-model="status"
                @change="setAutoTextStatus"
                active-color="#13ce66"
                :active-value="1"
                :inactive-value="0"
                active-text="显示状态"/>
            <el-col class="btn">
                <el-button type="primary" size="primary" @click="setAutoText" >发布</el-button>
            </el-col>
        </el-col>
        <editor v-model="addContent" v-if="editorShow"></editor>
        
    </div>
</template>
<script>
import editor from '@/components/editor';
export default {
    components:{
        editor
        
    },
    data(){
        return{
            status:'',
            editorShow:true,
            addContent:''
        }
    },
    methods: {
        async setAutoTextStatus(){
            let res=await this.$api.setAutoTextStatus({
                status:this.status
            })
            if(res.data.level=='success'){
                this.$message.success(res.data.message)
            }else{
                this.$message.error(res.data.message)
            }
        },
        async setAutoText(){
            let res=await this.$api.setAutoText({content:this.addContent})
            if(res.data.level=='success'){
                this.$message.success(res.data.message);
                this.getAutoTextDetail()
            }else{
                this.$message.error(res.data.message)
            }
        },
        async getAutoTextDetail(){
            let res=await this.$api.getAutoTextDetail({user_token: window.localStorage.getItem('user_token')});
            if(res.data.level=='success'){
                this.status=+res.data.data.status
                this.addContent=res.data.data.content
            }else{
                this.$message.error(res.data.message)
            }
            this.editorShow=false;
            setTimeout(()=>{
                this.editorShow=true
            },200)
        }
    },
    created() {
        this.getAutoTextDetail()
    },
}
</script>
<style lang="scss" scoped>
    .header{
        display: flex;
        align-items: center;
        .btn{
            flex:1;
            text-align: right;
            margin-right: 80px;
        }
    }
</style>
